<template>
  <el-dialog v-model="visible" :width="600" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()">
      <el-form-item prop="sysOrgId" label="所属公司/车队">
        <ma-org-select v-model="dataForm.sysOrgId" placeholder="请选择"></ma-org-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="telephone">
        <el-input v-model="dataForm.telephone" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idNo">
        <el-input v-model="dataForm.idNo" placeholder="身份证号"></el-input>
      </el-form-item>
      <!-- <div class="item_line">
        <el-form-item label="所属车队" prop="teamId">
          <el-select v-model="dataForm.teamId" placeholder="请选择">
            <el-option v-for="car in carList" :key="car.id" :label="car.name" :value="car.id" />
          </el-select>
        </el-form-item>
        <div></div>
      </div> -->
      <el-form-item label="驾驶证照片" prop="licensePhoto">
        <el-upload
          class="avatar-uploader"
          :action="constant.uploadUrl"
          :headers="{
            Authorization: userStore.token
          }"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="dataForm.licensePhoto" :src="dataForm.licensePhoto" class="avatar" alt="" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useDriverApi, useDriverSubmitApi } from '@/api/biz/driver'
import { useTeamListApi } from '@/api/biz/team'
import constant from '@/utils/constant'
import { useUserStore } from '@/store/modules/user'
import { UploadProps } from 'element-plus'
const props = defineProps({
  message: String
})
const userStore = useUserStore()
const visible = ref(false)
const dataFormRef = ref()
const carList = ref<any[]>([])
console.log(props)
const dataForm = reactive({
  id: '',
  name: '',
  telephone: '',
  idNo: '',
  licensePhoto: '',
  teamId: '',
  sysOrgId: ''
})

const init = (id?: number) => {
  visible.value = true
  dataForm.id = ''

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }

  if (id) {
    getDriver(id)
  }
  getTeamList()
}

const getDriver = (id: number) => {
  useDriverApi(id).then((res) => {
    Object.assign(dataForm, res.data)
  })
}

const dataRules = ref({
  sysOrgId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '必填项不能为空', trigger: 'change' }],
  telephone: [{ required: true, message: '必填项不能为空', trigger: 'change' }]
})
const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
  console.log(URL.createObjectURL(uploadFile.raw!))
  dataForm.licensePhoto = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}

function getTeamList() {
  useTeamListApi(dataForm).then((res) => {
    carList.value = res.data
  })
}

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }

    useDriverSubmitApi(dataForm).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>

<style scoped>
.item_line {
  display: flex;
}
.item_line > div {
  flex: 1;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
